<template>
  <el-dialog :title="info.title" :visible.sync="info.isShow" @opened="opened">
    <el-form :model="form">
      <el-form-item label="上级分类" :label-width="formLabelWidth">
        <el-select v-model="form.pid">
          <el-option label="--请选择--" value></el-option>
          <el-option label="顶级菜单" :value="0"></el-option>
          <el-option
            v-for="item in tableData"
            :key="item.id"
            :label="item.catename"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分类名称" :label-width="formLabelWidth">
        <el-input v-model="form.catename" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="图片" :label-width="formLabelWidth">
        <el-upload class="avatar-uploader" action="#" :show-file-list="false" :on-change="submit">
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-switch
          v-model="form.status"
          active-color="green"
          inactive-color="grey"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button v-if="info.isAdd" type="primary" @click="comfirm">确 定</el-button>
      <el-button v-else type="primary" @click="change">修 改</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addCate } from "../../../request/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["info"],
  data() {
    return {
      imageUrl: "",
      // form: {
      //   pid: "", //上级分类编号
      //   catename: "", //商品分类名称
      //   img: "",
      //   status: 1 //状态1正常2禁用
      // },
      catelist: [],
      formLabelWidth: "120px",
    };
  },
  computed: {
    ...mapGetters({
      tableData: "cate/tableData",
      form: "cate/form"
    })
  },
  methods: {
    comfirm() {
      this.addcate();
      this.cancel();
      this.requestCateList();
    },
    change() {
      this.changecate();
      this.cancel();
      this.requestCateList();
    },
    ...mapActions({
      requestCateList: "cate/CateListActions",
      editOne: "cate/editActions",
      cancelList: "cate/cancelAction",
      changecate: "cate/changeAction",
      addcate: "cate/addActions"
    }),
    cancel() {
      this.info.isShow = false;
      this.info.isAdd = true;
      this.info.title = "添加";
      this.cancelList();
      this.imageUrl = "";
    },
    submit(file) {
      // 处理文件大小
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件大小不能超过2M!");
        return;
      }
      // console.log(file.name);
      // 处理文件格式
      let ext = [".png", ".jpeg", ".jpg", ".gif",".webp"];
      let extName = file.name.slice(file.name.lastIndexOf("."));
      if (!ext.some(item => item === extName)) {
        errorAlert("文件格式不正确,请重新上传");
        return;
      }
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.img = file.raw;
    },
    opened(){
      // console.log(this.form.img);
      this.imageUrl=this.$pregImg+`${this.form.img}`
      // console.log(this.imageUrl);
    },
  },

  mounted() {
    this.requestCateList();
    this.editOne();
  }
};
</script>

<style scoped>
.avatar-uploader >>> .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader >>> .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>